<?php
/**
 * LICENSE
 * 
 * [license information]
 * 
 * @category   Vi
 * @copyright  Copyright (c) 2009 visualidea.org
 * @license    http://license.visualidea.org
 * @version    v 1.0 2009-04-15
 */
require_once 'Vi/Model.php';
class Models_Comment extends Vi_Model
{ 
    /**
     * The primary key column or columns.
     * A compound key should be declared as an array.
     * You may declare a single-column primary key
     * as a string.
     *
     * @var mixed
     */
    protected $_primary = 'comment_id';
    
    /**
     * Constructor.
     *
     * Supported params for $config are:
     * - db              = user-supplied instance of database connector,
     *                     or key name of registry instance.
     * - name            = table name.
     * - primary         = string or array of primary key(s).
     * - rowClass        = row class name.
     * - rowsetClass     = rowset class name.
     * - referenceMap    = array structure to declare relationship
     *                     to parent tables.
     * - dependentTables = array of child tables.
     * - metadataCache   = cache for information from adapter describeTable().
     *
     * @param  mixed $config Array of user-specified config options, or just the Db Adapter.
     * @return void
     */
    public function __construct($config = array())
    {
        $this->_name = $this->_prefix . 'comment';
        return parent::__construct($config); 
    } 
	
	public function getAllCommentsWithCategory($condition = array(), $order = null, $count = null, $offset = null)
    {
        $select = $this->select()
                ->setIntegrityCheck(false)
                ->from(array('com' => $this->_name))
                ->join(array('scon' => $this->_prefix . 'scontent'), 'com.scontent_id = scon.scontent_id', array('scontent_category_id'=>'scontent_category_id'))
                ->join(array('cat' => $this->_prefix . 'scontent_category'), 'scon.scontent_category_id = cat.scontent_category_id',array('catname' => 'name'))
                ->join(array('cl' => $this->_prefix . 'scontent_lang'), 'com.scontent_id = cl.scontent_id',array('title' => 'title'))
                ->order($order)
                ->limit($count, $offset);
        /**
         * Conditions
         */
        if (null != @$condition['keyword']) {
            $select->where($this->getAdapter()->quoteInto('com.name LIKE ?', "%{$condition['keyword']}%"));
        }
        if (null != @$condition['scontent_category_id']) {
            $select->where("cat.scontent_category_id = ?", $condition['scontent_category_id']);
        }
//        echo '<pre>'; print_r($select);die;
        return $this->fetchAll($select)->toArray();
    }
	public function getCommentbyLangId($scontentLangId)
	{
		$scontentLangId = intval($scontentLangId);
		$query = "
				SELECT *
				FROM ( {$this->_prefix}scontent
					 ) AS s1
				JOIN ( SELECT * 
			  		   FROM {$this->_prefix}scontent_lang
			  		   WHERE scontent_lang_id = {$scontentLangId}
			  		 ) AS s2
					 
				ON s2.scontent_id = s1.scontent_id	 		  		 
	        ";
		return $this->_db->fetchRow($query);
	}
    
//	public function getAllCommentsWithContent()
//    {
//        $select = $this->select()
//                ->setIntegrityCheck(false)
//                ->from(array('c' => $this->_name))
//                ->join(array('cat' => $this->_prefix . 'scontent_category'), 'c.scontent_category_id = cat.scontent_category_id', array('catname' => 'name'));
//                ->join(array('cat' => $this->_prefix . 'scontent_category'), 'c.scontent_category_id = cat.scontent_category_id', array('catname' => 'name'))
//    	return $this->fetchAll($select);
//    }
	
// 	public function getAllComments($condition = array(), $order = null, $count = null, $offset = null)
//    {
//        $select = $this->select()
//                ->setIntegrityCheck(false)
//                ->from(array('c' => $this->_name))
////                ->join(array('sc' => $this->_prefix . 'scontent_category'), 's.scontent_category_id = sc.scontent_category_id', array('cname' => 'name'))
////                ->join(array('s' => $this->_prefix . 'scontent'), 'c.scontent_id = s.scontent_id', array('cname' => 'name'))
//                ->order($order)
//                ->limit($count, $offset);
//        /**
//         * Conditions
//         */
//        if (null != @$condition['keyword']) {
//            $select->join(array('sl' => $this->_prefix . 'scontent_lang'), 's.scontent_id = sl.scontent_id', array('title'))
//                    ->where($this->getAdapter()->quoteInto('sl.title LIKE ?', "%{$condition['keyword']}%"))
//                    ->group('s.scontent_id');
//        }
//        if (null != @$condition['scontent_category_id']) {
//            $select->where("s.scontent_category_id = ?", $condition['scontent_category_id']);
//        }
//        return $this->fetchAll($select)->toArray();
//    }
}